﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using GrupyzServices.BdAccess;

namespace PreBuildGrupyz.Routes
{
    public class DealRouteConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            if (routeDirection == RouteDirection.IncomingRequest)
            {
                try
                {
                    string[] str = values["id"].ToString().Split('-');
                    if (str.Count() != 2) return false;
                    int id = int.Parse(str.First());

                    DealRepository deal = new DealRepository();

                    if (!deal.VerificaDeal(id, str.Last())) return false;
                    

                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
            return false;
        }
    }
}